package android_testsuite.mytest.media;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

import android_testsuite.R;

/**
 * Created by renhui on 2015/4/23.
 */
public class MediaPlayerTestActivity extends Activity {
    private MediaPlayer mPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//     一、播放本应用资源的方式
        mPlayer = MediaPlayer.create(this, R.raw.media);
        mPlayer.start();

//      二、播放本地存储中的资源
//        File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "1.mp3");
//        if (!file.exists()) {
//            Toast.makeText(this, "要播放的内容没有找到", Toast.LENGTH_LONG).show();
//        }
//        mPlayer = new MediaPlayer();
//        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//        try {
//            mPlayer.setDataSource(Environment.getExternalStorageDirectory().toString() + File.separator + "1.mp3");
//            mPlayer.prepareAsync();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mp) {
//                mPlayer.start();
//            }
//        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayer.release();
        mPlayer = null;
    }
}
